import { CronJob } from 'cron'
import { sendFeishuBotMessage } from '@wangyjhh/utils'
import { post } from './request'

const db = useDatabase()

export class Scheduler {
    tasks: Map<string, any> = new Map()

    async init() {
        const { rows } = await db.sql`SELECT * FROM 'task-list'`;
        rows?.forEach((item) => {
            this.addTask(item)
        })
    }

    addTask(task_config: any) {
        if (typeof task_config.template_variables === 'string') {
            task_config.template_variables = JSON.parse(task_config.template_variables)
        }

        const task = async () => {
            const get_variable_handles = task_config.template_variables.map((item: any) => {
                return post(item)
            })
            const res = await Promise.all(get_variable_handles)
            const variable_merged = Object.assign({}, ...res)

            sendFeishuBotMessage({
                webhook: task_config.webhook,
                secret: task_config.secret,
                template_id: task_config.template_id,
                template_variable: variable_merged
            })
        }
        const job = new CronJob(task_config.cron, task, null, true)
        this.tasks.set(task_config.id, {
            config: task_config,
            job
        })
    }

    async runTask(task_id: string) {
        const task = this.tasks.get(task_id)
        if (task) {
            const get_variable_handles = task.config.template_variables.map((item: any) => {
                return post(item)
            })
            const res = await Promise.all(get_variable_handles)
            const variable_merged = Object.assign({}, ...res)

            sendFeishuBotMessage({
                webhook: task.config.webhook,
                secret: task.config.secret,
                template_id: task.config.template_id,
                template_variable: variable_merged
            })
        }
    }

    removeTask(task_id: string) {
        const task = this.tasks.get(task_id)
        if (task) {
            task.job.stop()
            this.tasks.delete(task_id)
        }
    }

    updateTask(task_config: any) {
        const task = this.tasks.get(task_config.id)
        if (task) {
            task.job.stop()
            this.tasks.delete(task_config.id)
            this.addTask(task_config)
        }
    }

    stopTask(task_id: string) {
        const task = this.tasks.get(task_id)
        if (task) {
            task.job.stop()
            task.config.status = 0
        }
    }

    startTask(task_id: string) {
        const task = this.tasks.get(task_id)
        if (task) {
            task.job.start()
            task.config.status = 1
        }
    }

    getTaskActiveStatus(task_id: string) {
        const task = this.tasks.get(task_id)
        if (task) {
            return task.job.isActive
        }
        return false
    }

    getTaskNextDates(task_id: string, count: number) {
        const task = this.tasks.get(task_id)
        if (task) {
            return task.job.nextDates(count)
        }
    }

    getTaskList() {
        return [...this.tasks].map((item) => item[1].config)
    }
}